package clockin

import (
	"context"
	"gitee.com/enjoy_5/sigin-rpc/internal/sever"
	pb "gitee.com/enjoy_5/sigin-rpc/proto/clockin"
	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/status"
)

type ServerClicking struct {
	pb.UnimplementedClockInServer
}

// 创建签到
func (s *ServerClicking) ClockInCreate(ctx context.Context, in *pb.ClockInReq) (*pb.ClockInResp, error) {
	_, err := sever.ClockInCreate(in)
	if err != nil {
		return nil, status.Errorf(codes.Internal, "internal error: %s", err)
	}
	return &pb.ClockInResp{}, nil
}

// 获取签到记录详情
func (s *ServerClicking) GetClockIn(ctx context.Context, in *pb.GetClockInReq) (*pb.GetClockInResp, error) {
	stu, err := sever.GetClock(in)
	if err != nil {
		return nil, status.Errorf(codes.Internal, "internal error: %s", err)
	}
	var clock []*pb.Info
	for _, v := range stu {
		clock = append(clock, &pb.Info{
			UserId: v.UserId,
			Count:  v.Count,
		})
	}
	return &pb.GetClockInResp{
		Info: clock,
	}, nil
}

// 创建补签
func (s *ServerClicking) RetroactiveClockIn(ctx context.Context, in *pb.RetroactiveClockInReq) (*pb.RetroactiveClockInResp, error) {
	_, err := sever.RetroactiveClockIn(in)
	if err != nil {
		return nil, status.Errorf(codes.Unimplemented, "method RetroactiveClockIn not implemented")
	}
	return &pb.RetroactiveClockInResp{}, nil
}

// 查询用户签到详情
func (s *ServerClicking) GetClockInfo(_ context.Context, req *pb.GetClockInfoReq) (resp *pb.GetClockInfoResp, err error) {
	userStatus, err := sever.GetUserStatus(req)
	if err != nil {
		return nil, status.Errorf(codes.Unimplemented, "没有用户数据")
	}
	return &pb.GetClockInfoResp{Info: userStatus}, nil
}
